<?php
class AdminController extends Zend_Controller_Action
{
    protected $_manager;
    
    protected $_user;
    
    public function init()
    {
    	$this->_manager = new Default_Model_Managers_Recipes();
        $this->_helper->layout->setLayout('admin');
        $user = Zend_Auth::getInstance()->getStorage()->read();
        $this->_user = $user;
        if ($user instanceof Default_Model_Entities_Profile) {
        	$roleType = $user->getRoleType();
        }
        
        if (!isset($roleType) || $roleType !== 'Administrator') {
        	$this->getHelper('viewRenderer')->setNoRender();
        }
    }
    
    public function indexAction()
    {
        
    }
    
	public function addAction()
    {
        
    }
    
	public function editAction()
    {
        
    }
    
    public function loginformAction()
    {
        $this->view->user = Zend_Auth::getInstance()->getStorage()->read();
        
        $loginForm = new Default_Form_Login($_POST);
        $this->view->loginForm = $loginForm;
        
        $this->render();
    }
    
    public function loginAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper("layout")->disableLayout();
        
        $login = $this->getRequest()->login;
        $password = $this->getRequest()->password;
        $loginForm = new Default_Form_Login($_POST);
        
        if ($loginForm->isValid($_POST)) {
            $auth = Zend_Auth::getInstance();
            $authAdapter = new Default_Model_Managers_Auth($login, $password);
            $result = $auth->authenticate($authAdapter);
            
            switch ($result->getCode()) {
             
                case Zend_Auth_Result::FAILURE:
                    //echo 'Ошибка';
                    break;
             
                case Zend_Auth_Result::SUCCESS:
                    $usersManager = new Default_Model_Managers_Users;
                    $user = $usersManager->getProfileByLogin($login);
                    Zend_Auth::getInstance()->getStorage()->write($user);
                    break;
             
                default:
                    //echo 'Ошибка';
                    break;
            }
        }

        $redirectorHelper = $this->_helper->redirector;
        $redirectorHelper->gotoUrl($this->_request->getServer('HTTP_REFERER'));
    }
    
    public function logoutAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper("layout")->disableLayout();
        
        Zend_Auth::getInstance()->getStorage()->clear();
        $redirectorHelper = $this->_helper->redirector;
        $redirectorHelper->gotoUrl($this->_request->getServer('HTTP_REFERER'));
    }
    
    public function recipeformAction()
    {
    	
    	$recipeForm = new Default_Form_Recipe();
        $this->view->recipeForm = $recipeForm;
        
        $recipeId = Zend_Controller_Front::getInstance()->getRequest()->recipeId;
        
        if ($recipeId == true) {
        	$data = $this->_manager->getRecipeById($recipeId);
        	$recipeForm->isValid($data);
        	$this->view->photo = $data['photo'];
        	$this->view->ingredientsPhoto = $data['ingredients_photo'];  
        }
        $data = $_POST;
        $data['author_id'] = $this->_user->getId();
        
        if (isset($_POST['save']) && $recipeForm->isValid($data)) {
        	$values = $recipeForm->getValues();
        	$values['photo'] = $_FILES['photo'];
        	$values['ingredientsPhoto'] = $_FILES['ingredientsPhoto'];
        	if ( $recipeId == false ) {
        		if( $recipeId = $this->_manager->addRecipe($values) ){
        			$redirectorHelper = $this->_helper->redirector;
        			$redirectorHelper->gotoRoute(array('action' => 'edit', 'recipeId' => $recipeId), 'adminRoute');
        		}
        	} else {
        		$values['id'] = $recipeId; 
        		$this->_manager->updateRecipe($values);
        	}
        }
        
        $foodTypes = $this->_manager->getFoodTypes();
        $this->view->foodTypes = $foodTypes;
    }
}